package com.ruoyi.btlfbreed.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.btlfbreed.domain.TbEquipmentRecords;
import com.ruoyi.btlfbreed.mapper.TbEquipmentMapper;
import com.ruoyi.btlfbreed.domain.TbEquipment;
import com.ruoyi.btlfbreed.service.ITbEquipmentService;

/**
 * 设备信息Service业务层处理
 * 
 * @author codeman
 * @date 2024-05-03
 */
@Service
public class TbEquipmentServiceImpl implements ITbEquipmentService 
{
    @Autowired
    private TbEquipmentMapper tbEquipmentMapper;

    /**
     * 查询设备信息
     * 
     * @param equipmentId 设备信息主键
     * @return 设备信息
     */
    @Override
    public TbEquipment selectTbEquipmentByEquipmentId(Long equipmentId)
    {
        return tbEquipmentMapper.selectTbEquipmentByEquipmentId(equipmentId);
    }

    /**
     * 查询设备信息列表
     * 
     * @param tbEquipment 设备信息
     * @return 设备信息
     */
    @Override
    public List<TbEquipment> selectTbEquipmentList(TbEquipment tbEquipment)
    {
        return tbEquipmentMapper.selectTbEquipmentList(tbEquipment);
    }

    /**
     * 新增设备信息
     * 
     * @param tbEquipment 设备信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbEquipment(TbEquipment tbEquipment)
    {
        tbEquipment.setCreateTime(DateUtils.getNowDate());
        int rows = tbEquipmentMapper.insertTbEquipment(tbEquipment);
        insertTbEquipmentRecords(tbEquipment);
        return rows;
    }

    /**
     * 修改设备信息
     * 
     * @param tbEquipment 设备信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbEquipment(TbEquipment tbEquipment)
    {
        tbEquipment.setUpdateTime(DateUtils.getNowDate());
        tbEquipmentMapper.deleteTbEquipmentRecordsByEquipmentId(tbEquipment.getEquipmentId());
        insertTbEquipmentRecords(tbEquipment);
        return tbEquipmentMapper.updateTbEquipment(tbEquipment);
    }

    /**
     * 批量删除设备信息
     * 
     * @param equipmentIds 需要删除的设备信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbEquipmentByEquipmentIds(Long[] equipmentIds)
    {
        tbEquipmentMapper.deleteTbEquipmentRecordsByEquipmentIds(equipmentIds);
        return tbEquipmentMapper.deleteTbEquipmentByEquipmentIds(equipmentIds);
    }

    /**
     * 删除设备信息信息
     * 
     * @param equipmentId 设备信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbEquipmentByEquipmentId(Long equipmentId)
    {
        tbEquipmentMapper.deleteTbEquipmentRecordsByEquipmentId(equipmentId);
        return tbEquipmentMapper.deleteTbEquipmentByEquipmentId(equipmentId);
    }

    /**
     * 新增设备维修记录信息
     * 
     * @param tbEquipment 设备信息对象
     */
    public void insertTbEquipmentRecords(TbEquipment tbEquipment)
    {
        List<TbEquipmentRecords> tbEquipmentRecordsList = tbEquipment.getTbEquipmentRecordsList();
        Long equipmentId = tbEquipment.getEquipmentId();
        if (StringUtils.isNotNull(tbEquipmentRecordsList))
        {
            List<TbEquipmentRecords> list = new ArrayList<TbEquipmentRecords>();
            for (TbEquipmentRecords tbEquipmentRecords : tbEquipmentRecordsList)
            {
                tbEquipmentRecords.setEquipmentId(equipmentId);
                list.add(tbEquipmentRecords);
            }
            if (list.size() > 0)
            {
                tbEquipmentMapper.batchTbEquipmentRecords(list);
            }
        }
    }
}
